/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.crm.workorder.entity.WorkOrder
   Module Description   :

   Date Created      : 2009/11/20
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.crm.workorder.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.entity.CommonEntity;
import com.yaodian100.crm.cs.entity.Question;
import com.yaodian100.crm.workflow.entity.WorkFlowNode;

/**
 * @author andy.chuang
 * 
 */
@Entity
@Table(name = "CRM_WORK_ORDER")
public class WorkOrder extends CommonEntity {
	public enum FINISH_TYPE {
		SELF("1"), TRANSFER("2"), PROXY("3");
		FINISH_TYPE(String i) {
			value = i;
		}

		private String value;

		public String value() {
			return value;
		}

	}

	public enum APPROVE_STATUS {
		INIT(0), APPROVED(1), REJECT(2);
		APPROVE_STATUS(int i) {
			value = i;
		}

		private int value;

		public int value() {
			return value;
		}
	}

	public enum PRIORITY {
		LETTERS(0, 600, 60), NORMAL(1, 180, 48), HURRY(2, 60, 36), EMERGENCY(3, 30, 24);
		PRIORITY(int code, int minutes, int hours) {
			this.code = code;
			this.minutes = minutes;
			this.hours = hours;
		}

		private int hours;
		private int minutes;

		public int minutes() {
			return minutes;
		}

		private int code;

		public int code() {
			return code;
		}

		public int hours() {
			return hours;
		}
	}

	public enum STATUS {
		INIT(0), ANSWER_NEED_CONFIRM(1), SUCCESS(2), FORWARDED(3), RETURNED(4), NOT_ACTIVE(9);
		private STATUS(int i) {
			value = i;
		}

		private int value;

		public int value() {
			return value;
		}
	}

	/**
     * 
     */
	private static final long serialVersionUID = 8497994207558875590L;
	private Long id;
	private String shortDesc;
	private String content;
	// 前單備註
	private String comment;
	// 備註
	private String note;
	private WorkOrder parent;
	private Integer status;
	// private WorkOrderType workOrderType;
	private String domainId;
	private Integer priority;
	private User owner;
	private String responseGroup;
	private WorkFlowNode node;
	// 审核状态（如果需审核，预设为已审核）
	private Integer approveStatus = 1;
	private String approver;
	private String approveGroup;
	private String approveComment;
	private Date startTime;
	private Date finishTime;
	private Date forwardFinishTime;
	private String processLog;
	private String finishType;
	private Date approveTime;
	private String createGroup;
	private Integer endPriority;
	private String endClass1;
	private String endClass2;
	private String endClass3;
	private String approveId;
	private WorkOrder fromWorkOrder;
	private String compensation;

	/**
	 * @return the fromWorkOrder
	 */
	@ManyToOne
	@JoinColumn(name = "FROM_WORK_ORDER_ID")
	public WorkOrder getFromWorkOrder() {
		return fromWorkOrder;
	}

	/**
	 * @param fromWorkOrder the fromWorkOrder to set
	 */
	public void setFromWorkOrder(WorkOrder fromWorkOrder) {
		this.fromWorkOrder = fromWorkOrder;
	}

	/**
	 * @return the approveId
	 */
	@Column(name = "APPROVE_ID", length = 32)
	public String getApproveId() {
		return approveId;
	}

	/**
	 * @param approveId the approveId to set
	 */
	public void setApproveId(String approveId) {
		this.approveId = approveId;
	}

	/**
	 * @return the endPriority
	 */
	@Column(name = "END_PRIORITY")
	public Integer getEndPriority() {
		return endPriority;
	}

	/**
	 * @param endPriority the endPriority to set
	 */
	public void setEndPriority(Integer endPriority) {
		this.endPriority = endPriority;
	}

	/**
	 * @return the endClass1
	 */
	@Column(name = "END_CLASS1", length = 32)
	public String getEndClass1() {
		return endClass1;
	}

	/**
	 * @param endClass1 the endClass1 to set
	 */
	public void setEndClass1(String endClass1) {
		this.endClass1 = endClass1;
	}

	/**
	 * @return the endClass2
	 */
	@Column(name = "END_CLASS2", length = 32)
	public String getEndClass2() {
		return endClass2;
	}

	/**
	 * @param endClass2 the endClass2 to set
	 */
	public void setEndClass2(String endClass2) {
		this.endClass2 = endClass2;
	}

	/**
	 * @return the endClass3
	 */
	@Column(name = "END_CLASS3", length = 32)
	public String getEndClass3() {
		return endClass3;
	}

	/**
	 * @param endClass3 the endClass3 to set
	 */
	public void setEndClass3(String endClass3) {
		this.endClass3 = endClass3;
	}

	/**
	 * @return the createGroup
	 */
	@Column(name = "CREATE_GROUP")
	public String getCreateGroup() {
		return createGroup;
	}

	/**
	 * @param createGroup the createGroup to set
	 */
	public void setCreateGroup(String createGroup) {
		this.createGroup = createGroup;
	}

	/**
	 * @return the finishType
	 */
	@Column(name = "FINISH_TYPE", length = 1)
	public String getFinishType() {

		if (finishType == null) {

			User user = AdminHelper.getUser();
			User owner = this.getOwner();
			if (user == null || !(owner != null && user.getUsername().equals(owner.getUsername()))) {
				finishType = WorkOrder.FINISH_TYPE.PROXY.value;
			} else {
				finishType = WorkOrder.FINISH_TYPE.SELF.value;
			}
		}
		return finishType;
	}

	/**
	 * @param finishType the finishType to set
	 */
	public void setFinishType(String finishType) {
		this.finishType = finishType;
	}

	/**
	 * @return the approveTime
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "APPROVE_TIME")
	public Date getApproveTime() {
		return approveTime;
	}

	/**
	 * @param approveTime the approveTime to set
	 */
	public void setApproveTime(Date approveTime) {
		this.approveTime = approveTime;
	}

	/**
	 * @return the processLog
	 */
	public String getProcessLog() {
		return processLog;
	}

	/**
	 * @param processLog the processLog to set
	 */
	public void setProcessLog(String processLog) {
		this.processLog = processLog;
	}

	/**
	 * @return the forwardFinishTime
	 */
	public Date getForwardFinishTime() {
		return forwardFinishTime;
	}

	/**
	 * @param forwardFinishTime the forwardFinishTime to set
	 */
	public void setForwardFinishTime(Date forwardFinishTime) {
		this.forwardFinishTime = forwardFinishTime;
	}

	/**
	 * @return the startTime
	 */
	public Date getStartTime() {
		if (startTime != null) {
			return startTime;
		} else {
			if (owner != null) {
				startTime = new Date();
			}
			return startTime;
		}
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	/**
	 * @return the finishTime
	 */
	public Date getFinishTime() {
		if (finishTime != null) {
			return finishTime;
		} else {
			if (status == 2) {
				finishTime = new Date();
			}
			return finishTime;
		}
	}

	/**
	 * @param finishTime the finishTime to set
	 */
	public void setFinishTime(Date finishTime) {
		this.finishTime = finishTime;
	}

	/**
	 * @return the approveComment
	 */
	@Column(length = 500)
	public String getApproveComment() {
		return approveComment;
	}

	/**
	 * @param approveComment the approveComment to set
	 */
	public void setApproveComment(String approveComment) {
		this.approveComment = approveComment;
	}

	/**
	 * @return the approver
	 */
	@Column(length = 20)
	public String getApprover() {
		return approver;
	}

	/**
	 * @param approver the approver to set
	 */
	public void setApprover(String approver) {
		this.approver = approver;
	}

	/**
	 * @return the approveStatus
	 */
	public Integer getApproveStatus() {
		return approveStatus;
	}

	/**
	 * @param approveStatus the approveStatus to set
	 */
	public void setApproveStatus(Integer approveStatus) {
		this.approveStatus = approveStatus;
	}

	/**
	 * @return the note
	 */
	@Column(length = 4000)
	public String getNote() {
		return note;
	}

	/**
	 * @param note the note to set
	 */
	public void setNote(String note) {
		this.note = note;
	}

	/**
	 * @return the comment
	 */
	@Column(length = 4000, name = "ORDER_COMMENT")
	public String getComment() {
		return comment;
	}

	/**
	 * @param comment the comment to set
	 */
	public void setComment(String comment) {
		this.comment = comment;
	}

	/**
	 * @return the node
	 */
	@ManyToOne
	public WorkFlowNode getNode() {
		return node;
	}

	/**
	 * @param node the node to set
	 */
	public void setNode(WorkFlowNode node) {
		this.node = node;
	}

	/**
	 * @return the owner
	 */
	@ManyToOne
	public User getOwner() {
		return owner;
	}

	/**
	 * @param owner the owner to set
	 */
	public void setOwner(User owner) {
		this.owner = owner;
	}

	/**
	 * @return the priority
	 */
	public Integer getPriority() {
		return priority;
	}

	/**
	 * @param priority the priority to set
	 */
	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	/**
	 * @return the domainId
	 */
	@Column(length = 32, name = "DOMAINID")
	public String getDomainId() {
		return domainId;
	}

	private Question question;

	public void setQuestion(Question question) {
		this.question = question;
	}

	@ManyToOne
	@JoinColumn(name = "DOMAINID", insertable = false, updatable = false)
	public Question getQuestion() {
		return this.question;
	}

	/**
	 * @param domainId the domainId to set
	 */
	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}

	// /**
	// * @return the workOrderType
	// */
	// @ManyToOne
	// public WorkOrderType getWorkOrderType() {
	// return workOrderType;
	// }
	//
	// /**
	// * @param workOrderType the workOrderType to set
	// */
	// public void setWorkOrderType(WorkOrderType workOrderType) {
	// this.workOrderType = workOrderType;
	// }

	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_crm_work_order")
	@SequenceGenerator(name = "seq_crm_work_order", sequenceName = "SEQ_CRM_WORK_ORDER")
	@Column(name = "WORK_ORDER_ID")
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the shortDesc
	 */
	@Column(length = 500)
	public String getShortDesc() {
		return shortDesc;
	}

	/**
	 * @param shortDesc the shortDesc to set
	 */
	public void setShortDesc(String shortDesc) {
		this.shortDesc = shortDesc;
	}

	/**
	 * @return the content
	 */
	@Column(length = 4000)
	public String getContent() {
		return content;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the parent
	 */
	@ManyToOne
	public WorkOrder getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(WorkOrder parent) {
		this.parent = parent;
	}

	/**
	 * @return the status
	 */
	public Integer getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(Integer status) {
		this.status = status;
	}

	/**
	 * @return the responseGroup
	 */
	@Column(length = 50)
	public String getResponseGroup() {
		return responseGroup;
	}

	/**
	 * @param responseGroup the responseGroup to set
	 */
	public void setResponseGroup(String responseGroup) {
		this.responseGroup = responseGroup;
	}

	/**
	 * @return the approveGroup
	 */
	@Column(length = 50)
	public String getApproveGroup() {
		return approveGroup;
	}

	/**
	 * @param approveGroup the approveGroup to set
	 */
	public void setApproveGroup(String approveGroup) {
		this.approveGroup = approveGroup;
	}

	@Column(length = 4000)
	public String getCompensation() {
		return compensation;
	}

	public void setCompensation(String compensation) {
		this.compensation = compensation;
	}
	
	
}
